package com.ruoyi.clf.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.clf.mapper.AddressMapper;
import com.ruoyi.clf.domain.Address;
import com.ruoyi.clf.service.IAddressService;

/**
 * 收货地址Service业务层处理
 * 
 * @author clf
 * @date 2025-05-17
 */
@Service
public class AddressServiceImpl implements IAddressService 
{
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public Address selectAddressById(Integer id)
    {
        return addressMapper.selectAddressById(id);
    }

    /**
     * 查询收货地址列表
     * 
     * @param address 收货地址
     * @return 收货地址
     */
    @Override
    public List<Address> selectAddressList(Address address)
    {

        return addressMapper.selectAddressList(address);
    }

    /**
     * 新增收货地址
     * 
     * @param address 收货地址
     * @return 结果
     */
    @Override
    public int insertAddress(Address address)
    {
        Long defaultss = address.getDefaultss();
        if (defaultss!=null && defaultss==1){
//            若是设为默认
            Long uid = address.getUid();
            List<Address> addresses = addressMapper.selectAddressByUserId(uid);

            for (Address address2 : addresses) {
//                把其他的设为0
                address2.setDefaultss(0l);
                addressMapper.updateAddress(address2);
            }
//            把当前的设为1
            address.setDefaultss(1l);
        }
        return addressMapper.insertAddress(address);
    }

    /**
     * 修改收货地址
     * 
     * @param address 收货地址
     * @return 结果
     */
    @Override
    public int updateAddress(Address address)
    {
        Long defaultss = address.getDefaultss();
        if (defaultss!=null && defaultss==1){
//            若是设为默认
            Long uid = address.getUid();
            List<Address> addresses = addressMapper.selectAddressByUserId(uid);

            for (Address address2 : addresses) {
//                把其他的设为0
                address2.setDefaultss(0l);
                addressMapper.updateAddress(address2);
            }
//            把当前的设为1
            address.setDefaultss(1l);
        }
        return addressMapper.updateAddress(address);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Integer[] ids)
    {
        return addressMapper.deleteAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressById(Integer id)
    {
        return addressMapper.deleteAddressById(id);
    }

    @Override
    public List<Address> selectAddressByUid(Long uid) {
        return addressMapper.selectAddressByUserId(uid);
    }


}
